//
//  PayViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/6/30.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "PayViewController.h"
#import "PayTableViewCell.h"
#import "PayMoneyCountViewController.h"

@interface PayViewController ()<UITableViewDelegate,UITableViewDataSource,UIPickerViewDelegate,UIPickerViewDataSource>{
    NSInteger _bankIndex;   // 银行选择 记录
}


@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, strong)UITableView * tableView;


@property (nonatomic, strong)NSArray * dataArray;
@property (nonatomic, strong)UIView * bgView;
@property (nonatomic, strong)UIPickerView * pickerView;

@end

@implementation PayViewController


- (void)viewWillAppear:(BOOL)animated{
    
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _bankIndex = 0;
    
    [self setupNavBar];
    [self setupUI];
//    [self setupPivkerView];
}

- (void)setupNavBar{
    
    self.title = @"入金";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}

- (void)loadData{
    
    [[NetworkTool sharedTools] POST:@"user/bank-list" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        self.dataArray = responseObject[@"param"];

        [self.pickerView reloadAllComponents];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
    
}

- (void)setupUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 * SCREENH6, SCREEN_WIDTH, 195 * SCREENH6)];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = NO;
    
    [self.tableView registerClass:[PayTableViewCell class] forCellReuseIdentifier:@"PayCell"];
    
    [self.view addSubview:self.tableView];
    
}


- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
}



- (void)setupPivkerView{
    
    self.bgView = [[UIView alloc]init];//SCREENH_HEIGHT
    self.bgView.frame = CGRectMake(0, SCREENH_HEIGHT, SCREEN_WIDTH, SCREENH_HEIGHT / 3);
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bgView];
    
    UIView *btnView = [[UIView alloc]init];
    btnView.frame = CGRectMake(0, 0, self.view.frame.size.width, 40 * SCREENH6);
    btnView.backgroundColor = [UIColor lightGrayColor];
    [self.bgView addSubview:btnView];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(10 * SCREENH6, 5 * SCREENH6, 60 * SCREENH6, 30 * SCREENH6);
    [sureBtn setTitle:@"取消" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(cancelBtn) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:sureBtn];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(self.view.frame.size.width - 70 * SCREENH6, 5 * SCREENH6, 60 * SCREENH6, 30 * SCREENH6);
    [cancelBtn setTitle:@"确定" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(sureBtn) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:cancelBtn];
    
    float height = SCREENH_HEIGHT / 3 - 40 * SCREENH6;
    self.pickerView = [[UIPickerView alloc]init];
    self.pickerView.frame = CGRectMake(0, 40 * SCREENH6, SCREEN_WIDTH, height);
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.bgView addSubview:self.pickerView];
}


// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// 每列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return self.dataArray.count;
}


// 返回每一行的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    NSDictionary * dict = self.dataArray[row];
    
    NSString * bankNo = dict[@"bankNo"];
    NSString * bankName = dict[@"bankName"];
    _bankIndex = row;
    
    NSString * bankInfo;
    
    if (bankNo.length == 19) {
        
        NSString * bankNum = [bankNo substringFromIndex:15];
        
        bankInfo = [NSString stringWithFormat:@"%@(尾号%@)",bankName,bankNum];
        
    }else{
        NSString * bankNum = [bankNo substringFromIndex:12];
        
        bankInfo = [NSString stringWithFormat:@"%@(尾号%@)",bankName,bankNum];
        
    }
    
    return bankInfo;
}



//- (void)cancelBtn{
//    
//    [UIView animateWithDuration:0.5 animations:^{
//        self.bgView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, SCREENH_HEIGHT / 3);
//    } completion:^(BOOL finished) {
//    }];
//
//}



//- (void)sureBtn{
//    
//    NSDictionary * dict = self.dataArray[_bankIndex];
//    
//    NSString * idString = [NSString stringWithFormat:@"%@",dict[@"id"]];
//    
//    self.hidesBottomBarWhenPushed = YES;
//    
//    PayMoneyCountViewController * payMoneyVC = [PayMoneyCountViewController new];
//    
//    payMoneyVC.bankID = idString;
//    
//    [self.navigationController pushViewController:payMoneyVC animated:YES];
//    
//    
//    self.bgView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, SCREENH_HEIGHT / 3);
//    
//}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PayCell" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        
        cell.tltleLabel.text = @"银行卡支付";
        cell.subTltleLabel.text = @"无需开通网银";
        cell.iconImageView.image = [UIImage imageNamed:@"银行卡支付"];
        
    }else if (indexPath.row == 1){
        
        cell.tltleLabel.text = @"支付宝转账";
        cell.subTltleLabel.text = @"手机支付，无需手续费";
        cell.iconImageView.image = [UIImage imageNamed:@"支付宝转账"];
        
    }else{
        
        cell.tltleLabel.text = @"微信转账";
        cell.subTltleLabel.text = @"极简支付流程";
        cell.iconImageView.image = [UIImage imageNamed:@"微信转账"];
    }
    
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 65 * SCREENH6;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//        [UIView animateWithDuration:0.5 animations:^{
//            self.bgView.frame = CGRectMake(0, SCREENH_HEIGHT - SCREENH_HEIGHT / 3, SCREEN_WIDTH, SCREENH_HEIGHT / 3);
//        }];
        
        [[NetworkTool sharedTools] POST:@"pay/check" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
            
            if ([code isEqualToString:@"0"]) {
                
                self.hidesBottomBarWhenPushed = YES;
                
                PayMoneyCountViewController * payMoneyVC = [PayMoneyCountViewController new];
                
                //        payMoneyVC.bankID = idString;
                
                [self.navigationController pushViewController:payMoneyVC animated:YES];
                
            }else{
              
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"服务器内部错误，请稍后再试！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
        
    }else{
        
//        PayMoneyCountViewController * payMoneyVC = [PayMoneyCountViewController new];
//        
//        [self.navigationController pushViewController:payMoneyVC animated:YES];
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"目前还未开通，敬请期待！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
}



- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
